<?php
session_start();
if (!isset($_POST['do'])) {
    exit;
}
?>
<script src="inc/js/core.js" type="text/javascript"></script>
<script src="inc/js/jquery.timeago.js" type="text/javascript"></script>

<?php
require ('./inc/database.php');
require ('./inc/functions.php');
require ('./config.php');
require_once('./class/class.phpmailer.php');
require_once('./class/class.user.php');
require_once('./class/class.project.php');
require_once('./class/class.task.php');
require_once('./class/class.project_access_details.php');
require_once('./class/class.permissions.php');
require_once('./class/class.comments.php');
require_once('./class/class.dashboard.php');
db_connect();
if ($_POST['do'] == 'show_project_ajax') {// בעת לחיצה על פרוייקט נפתח כל המידע עליו וכל הפניות
    $id = $_POST['id'];
    $session = $_POST['name_session']; // המשתמש שמחובר (תוכנתן ,איש קשר ..
    $proj = new project();
    $bool = $proj->getby_id($id);
    $_SESSION['project'] = $proj->getid();
    $user = new user();
    $user->getby_name($session);
    ?>
    <span id="name_project" class="color_w"><?php echo $proj->project_name ?></span>
    <?php $proj->get_html_project(); ?><!-- פרטיים של הפרוייקט-->
    <input id="id_project" name="id_project" type="hidden" value="<?php echo $proj->getid(); ?>"/>
    <?php $proj->selector(); ?>
    <div id="all_task">
        <?php
        $task = new task();
        $arr = $proj->get_tasks(100); //מערך של כל הפניות
        if ($arr != 0) {
            foreach ($arr as $task) {
                $task->getby_id($id_task);
                //if($task->task_status_id!=1) {
                $task = task::createTaskFromArray($task);
                $task->get_html_task();
                }
            }
        //}
        ?>
    </div>
    <script>

        var x =<?php echo $user->role_type; ?>;
        $('#span_send_task').css('display', 'block');
    </script>   
    <?php
}//end show_project_ajax


if ($_POST['do'] == 'show_project_ajax_find') {// בעת לחיצה על פרוייקט נפתח כל המידע עליו וכל הפניות
    $name_project = htmlspecialchars_decode($_POST['name_project'], ENT_COMPAT);
    $session = $_POST['name_session']; // המשתמש שמחובר (תוכנתן ,איש קשר ..
    $proj = new project();

    $bool = $proj->getby_name($name_project);

    $_SESSION['project'] = $proj->getid();
    $user = new user();
    $user->getby_name($session);

    if (!$bool || ($proj->project_status == 1 && !($user->role_type == 1 || $user->role_type == 6)) || !(permissions::is_permissions($user->getuser_id(), $proj->getid()) ) && !($user->role_type == 1)) {
        ?>
        <label> אין פרוייקט כזה במערכת</label> <?php
        exit;
    }
    ?>
    <span id="name_project" class="color_w"><?php echo $proj->project_name ?></span>
    <?php $proj->get_html_project(); ?><!-- פרטיים של הפרוייקט-->

    <input id="id_project" name="id_project" type="hidden" value="<?php echo $proj->getid(); ?>"/>
    <?php $proj->selector(); ?>
    <div id="all_task">
        <?php
        $task = new task();
        $arr = $proj->get_tasks(); //מערך של כל הפניות
        if ($arr != 0) {
            foreach ($arr as $task) {
                //$task->getby_id($id_task);
                //if($task->task_status_id!=1) {
                $task = task::createTaskFromArray($task);
                $task->get_html_task(); //מדפיס פנייה עם כל התגובות 
                //}
            }
        }
        ?>

    </div>
    <script>
        $(document).ready(function(e) {
            $('#hid_id_project').val($('#id_project').val());
            var x =<?php echo $user->role_type; ?>;
            if (x == 2 || x == 3)
            {
                $('#span_send_task').css('display', 'none');
            }
            else
            {
                $('#span_send_task').css('display', 'block');
            }
        });
    </script>
    <?php
}//end show_project_ajax_find
if ($_POST['do'] == 'selector_all_option') {//מחזיר את כל הפניות לאותו פרוייקט לפי סטאטוס ,וזמן
    $id_project = $_POST['id_project'];
    $status = $_POST['status'];
    $from = $_POST['from'];
    $to = $_POST['to'];
    $project = new project();
    $project->getby_id($id_project);
    $arr_task = $project->get_tasks_in_option($status, $from, $to);
    if ($arr_task != 0) {
        foreach ($arr_task as $task) {
            //if($task->task_status_id!=1) {
            $task = task::createTaskFromArray($task);
            $task->get_html_task(); //מדפיס פנייה עם כל התגובות 
            //}
        }
    } else {
        echo "לא נמצאו פניות.... ";
    }
}//end selector_all_option
if ($_POST['do'] == 'new_message') {// בודק האם יש הודעות חדשות 
    $user_id = $_POST['user_id']; //המשתמש שמחובר

    $user = new user(); //יצירת אובייקט 
    $user->getby_id($user_id); //כל הפרטיים של המשתמש
    if ($user->role_type == 1 || $user->role_type == 6) {//האם אתה מנהל 
        $arr_project = project::getallproject();
    } else {
        $arr_project = $user->get_proj_permissions(); //return project_id of user
    }
    ?>

    <?php
    $count = 0; //מונה לספירת כמות ההודעות החדשות
    foreach ($arr_project as $P) {//לולאה שרצה על כל ההרשאות שיש  לאותו משתמש  (לפי פרוייקט
        $temp_project = new project(); //יצירת אובייקט
        $temp_project->getby_id($P); //מעדכן את האובייקט 
        $arr_task = $temp_project->get_tasks(); //מערך של כל הפניות לפי פרוייקט אם אין מחזיר 0

        if ($arr_task != 0) {
            foreach ($arr_task as $T) {//לולאה שרצה על הפניות לפי פרוייקט
                $temp_task = task::createTaskFromArray($T); //יצירת אובייקט
                $bool = if_job($user->getuser_id(), $temp_task->user_id);
                //echo $bool.''.$user->getuser_id().','.$temp_task->user_id.'<br />';
                if ($user->role_type == 4 || $user->role_type == 5)
                /* כאשר המנהל שולח פנייה רק התכנתנים והבודקים רואים אותה */ {
                    $user_meneger = new user();
                    $user_meneger->getby_id($temp_task->user_id);

                    if ($user_meneger->role_type == 1 || $user_meneger->role_type == 6) {
                        $bool = 1;
                    }
                }
                if ($temp_task->task_status_id == 2 && $bool == 0 && $temp_task->task_project_status != 1) {//בדיקה האם הפניה נקראה
                    ?> 
                    <div onclick="gotomassage('task',<?php echo $temp_task->get_task_id(); ?>, null,<?php echo $user_id ?>)">
                        <span id="task_icon" ></span>
                        <p class="p">
                            <span id="new_task" class="color_turkiz">
                                בפרוייקט 
                            </span> 
                            <span id="new_task_name"> <?php echo project::get_name($temp_task->project_id);
                    $count++;
                    ?> </span>
                            <span id="maet" class="color_turkiz"> מאת:</span>
                            <span><?php echo user::get_name($temp_task->user_id); ?> </span>
                            <span  id="customer" class="color_turkiz">לקוח:</span>
                            <span>
                                <?php
                                $project = new project();
                                $project->getby_id($temp_task->project_id);
                                echo $project->get_customer_name($project->project_customer);
                                ?>
                            </span>
                            <span id="timeago<?php echo $count; ?>" class="timeago  time" title="<?php echo $temp_task->task_date . ' ' . $temp_task->task_time; ?>">
                    <?php echo $temp_task->task_date . ' ' . $temp_task->task_time ?>
                            </span>
                        </p>
                        <script> $('#timeago<?php echo $count; ?>').timeago();</script>
                    </div><!-- כאן צריך להוסיף לרשימה של הודעות חדשות-->

                    <?php
                } else {
                    $temp_comments = $temp_task->get_comments(); //מחזיר את כל התגובות של הפניה 
                    if ($temp_comments != 0) {
                        foreach ($temp_comments as $C) {
                            $bool = if_job($user->getuser_id(), $C->user_id);

                            if ($C->read == 0 && $bool == 0) {
                                ?>
                                <div  onclick="gotomassage('comments',<?php echo $temp_task->get_task_id(); ?>,
                                <?php echo $C->getcomment_id(); ?>,<?php echo $user_id ?>)">
                                    <span id="comment_icon" ></span>
                                    <p class="p">
                                        <span id="new_comment" class="color_turkiz">
                                            בפרוייקט 
                                        </span> 
                                        <span><?php echo project::get_name($temp_project->getid());
                                $count++;
                                ?></span>
                                        <span id="from_comment" class="color_turkiz"> מאת:</span> 
                                        <span><?php echo user::get_name($C->user_id); ?> ,</span>
                                        <span id="customer" class="color_turkiz" >לקוח</span>
                                        <span>
                                            <?php echo $temp_project->get_customer_name($temp_project->project_customer); ?>
                                        </span>
                                        <span id="timeago_com<?php echo $count ?>" class="timeago time" title="<?php echo $C->comment_date . ' ' . $C->comment_time; ?>">
                                <?php echo $C->comment_date . ' ' . $C->comment_time; ?>
                                        </span>
                                    </p> 
                                    <script>$('#timeago_com<?php echo $count ?>').timeago();</script>
                                </div> <!--כאן להוסיף לרשימה את התגובות החדשות-->
                                <?php
                            }
                        }//end_foreach_temp comments
                    }//end_tempcomments
                }//end_else task id
            }//end_foreach arr task
        }//end_if arr task
    }//end_foreach arr project
    ?>
    <script>


        $(document).ready(function(e) {
            //$('#timeago').timeago();
            //$('#timeago_com').timeago();
            $('#hid_sound_new').val(<?php echo $count ?>)
    <?php if ($count == 0) { ?>
                $('#mass #contect').html("");
                $('#mass #bnt').css('background', 'url(./style/images/massage_off.png) no-repeat bottom right');
                $('#hid_mass').val('false');
    <?php } else { ?>
                $('#mass #bnt').css('background', 'url(./style/images/message_on.png) no-repeat bottom right');
                $('#mass  #contect').html(<?php echo $count; ?> + ' פניות חדשות מחכות ');//מספר ההודעות שלא נקראו 
                $('#hid_mass').val('true');
    <?php } ?>
        });
        function gotomassage(name_class, id_task, id_comment, user_id)
        {
            gomassage(name_class, id_task, id_comment, user_id);

        }
    </script>

    <?php
}//end new_message

if ($_POST['do'] == 'gomassage') {
    $user_id = $_POST['user_id'];
    $id_task = $_POST['id_task'];
    $name_class = $_POST['name_class'];
    $id_comment = $_POST['id_comment'];
    $task = new task();
    $task->getby_id($id_task);
    $task->get_html_task();
    $Oj_user = new user();
    $Oj_user->getby_id($user_id);
    if ($Oj_user->role_type == 1 || $Oj_user->role_type == 4) {
        
    } else {
        if ($name_class == 'comments') {
            $temp_comments = new comments();
            $temp_comments->getby_id($id_comment);
            $temp_comments->read = 1;
            $temp_comments->update();
        } else {
            $task->task_status_id = 3;

            $task->update();
        }
    }
}// end gomassage
if ($_POST['do'] == 'selector_all_option_customer') {
    $id_customer = $_POST['id_customer'];
    $from = $_POST['from'];
    $to = $_POST['to'];
    $arr_project = project::get_all_project_of_customer($id_customer);
    print_r($arr_project);
    exit;
    if ($arr_project != 0) {
        foreach ($arr_project as $single_project) {
            $arr_task = $single_project->get_tasks_in_option("", $from, $to);
            if ($arr_task != 0) {
                ?><span id="name_project" class="color_w"><?php echo $single_project->project_name ?></span><?php
                foreach ($arr_task as $task) {   //$task= new task();
                    //$task->getby_id($id_task);
                    //if($task->task_status_id!=1) {
                    $task = task::createTaskFromArray($task);
                    $task->get_html_task(); //מדפיס פנייה עם כל התגובות 
                    //}
                }
            } else {
                //echo "לא נמצאו פניות.... ";	
            }
        }
    } else {
        // echo "אין פרוייקטים לחברה ";  
    }
}//selector_all_option_customer
if ($_POST['do'] == 'get_all_info_customer') {
    $arr_project = project::get_all_project_of_customer($_POST['id_customer']);

    dashboard::get_html_bash();
    if ($arr_project != 0) {
        ?>

        <span id="name_customer" class="color_w"><?php echo $arr_project[0]['customer_name']; ?></span>
            <?php project::selector_customer($_POST['id_customer']); ?>
        <div id="all_project_customer">
            <?php
            $projectIndex = $arr_project[0]['project_id'];
            $firstRun = false;
            foreach ($arr_project as $proj) {
                ?> 	

                <?php
                if (!$projectIndex == $proj['project_id']) {
                    $projectIndex = $proj['project_id'];
                    if ($firstRun)
                        echo "</div>"; // close of all_task
                    ?>
                    <span id="name_project" class="color_w"><?php echo $proj['project_name'] ?></span>
                    <input id="id_customer" name="id_customer" type="hidden" value="<?php echo $_POST['id_customer']; ?>"/>
                    <div id="all_task">
                        <?php
                        $firstRun = true;
                    }//end if(!$projectIndex==$proj['project_id'])
                    //$task->getby_id($id_task);
                    //if($task->task_status_id!=1) {
                    $task = task::createTaskFromArray($proj);
                    $task->get_html_task();
                    //}
                }// end foreach
            }// if($arr_project!=0)
            else { // אם אין פרוייקטים 
                ?>   <span id="name_customer" class="color_w">אין פרויקטים</span><?php
            }
        }//get_all_info_customer
        if ($_POST['do'] == 'change_status_task') {
            $id_status = $_POST['id_status'];
            $id_task = $_POST['id_task'];
            $task = new task;
            $task->getby_id($id_task);
            if ($task->task_status_id != $id_status) {

                $stringstatus = sprintf("UPDATE `task` SET
						   `task_status_id`=$id_status 
							WHERE `task_id`=$id_task");
                $querystatus = mysql_query($stringstatus) or die(mysql_error() . ' ' . $stringstatus);

                $session = $_SESSION['user'];

                $user = new user;
                $user->getby_name($session);

                //$task = new task;
                //$task->getby_id($id_task);

                $project = new project;
                $project->getby_id($task->project_id);
                $permi = $project->getby_project_id(); // מחזיר מערך של כל ההרשאות שלו
                $name = user::get_name($task->user_id);
                $name_session = user::get_name($user->getuser_id());
                $name_project = project::get_name($task->project_id);
                $temp_id_task = $task->get_task_id();
                switch ($id_status) {
                    case 2:
                        $subject = "נפתחה פנייה $temp_id_task על ידי $name לפרוייקט $name_project";

                        break;
                    case 4:
                        $subject = " פנייה $temp_id_task נסגרה על ידי $name_session לפרוייקט $name_project";
                        break;
                    case 5:
                        $subject = " נדחתה פנייה $temp_id_task לפרוייקט $name_project ";
                        ;
                        break;
                    default:
                        $subject = " סטאטוס פנייה $temp_id_task השתנה בפרוייקט $name_project";
                }
                if ($permi) {
                    $mail = new PHPMailer();
                    foreach ($permi as $value) {
                        $temp_user = new user;
                        $temp_user->getby_id($value->user_id);
                        if ($temp_user->send_mail == 1 && $temp_user->role_type != 2 && $temp_user->role_type != 3) {
                            $mail->AddAddress("$temp_user->user_mail"); // הכתובת אליה נשלח המייל
                            $haveAddress = true;
                        }
                    }
                    $status1 = $task->get_name_status_task($task->task_status_id);
                    $status2 = $task->get_name_status_task($id_status);
                    $body = " הפנייה -$task->task_content  \n
			      השתנה ממצב $status1- למצב -$status2 ";

                    $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                    $mail->SMTPAuth = true;                  // enable SMTP authentication
                    $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                    $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                    $mail->Port = 465;                   // set the SMTP port

                    $mail->Username = "do_not_reply@restartit.co.il";  // GMAIL username
                    $mail->Password = "restart4mail";            // GMAIL password

                    $mail->FromName = "RestartIT CRM";
                    $mail->From = "restartit@email.com"; // כתובת השולח

                    $mail->Subject = $subject; // נושא הדוא"ל
                    $mail->Body = "$body";
                    $mail->WordWrap = 150; // אני מאפשר רק 50 תוים בשורה
                    $mail->CharSet = 'UTF-8';

                    //$mail->AddAttachment($temp,' תמונה לפני','base64','image/jpeg');
                    if ($haveAddress) {// אם יש כתובת לשליחה
                        /* if(!$mail->Send()) // כאן הדוא"ל נשלח
                          {
                          echo "Message was not sent";
                          echo "Mailer Error: " . $mail->ErrorInfo;
                          }
                          else { echo "Message was sent";} */
                    }
                }// end if($permi)
                /** send for admin */
                if ($user->role_type != 1) {
                    $arr_admin = user::get_user_by_type(1);
                    $mail = new PHPMailer();
                    foreach ($arr_admin as $value) {
                        //$temp_user = new user;
                        // $temp_user->getby_id($value->user_id);
                        if ($value->send_mail == 1) {
                            $mail->AddAddress("$value->user_mail"); // הכתובת אליה נשלח המייל
                            $haveAddress = true;
                        }
                    }

                    $status1 = $task->get_name_status_task($task->task_status_id);
                    $status2 = $task->get_name_status_task($id_status);
                    $body = " הפנייה -$task->task_content  \n
			      השתנה ממצב $status1- 	  למצב -$status2 ";

                    $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                    $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                    $mail->SMTPAuth = true;                  // enable SMTP authentication
                    $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                    $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                    $mail->Port = 465;                   // set the SMTP port

                    $mail->Username = "do_not_reply@restartit.co.il";  // GMAIL username
                    $mail->Password = "restart4mail";            // GMAIL password

                    $mail->FromName = "RestartIT CRM";
                    $mail->From = "restartit@email.com"; // כתובת השולח
                    //$mail->AddAddress("alonh@restartit.co.il"); // הכתובת אליה נשלח המייל
                    $mail->Subject = $subject; // נושא הדוא"ל
                    $mail->Body = "$body";
                    $mail->WordWrap = 150; // אני מאפשר רק 50 תוים בשורה
                    $mail->CharSet = 'UTF-8';

                    //$mail->AddAttachment($temp,' תמונה לפני','base64','image/jpeg');
                    if ($haveAddress) {// אם יש כתובת לשליחה
                        /* if(!$mail->Send()) // כאן הדוא"ל נשלח
                          {
                          echo "Message was not sent";
                          echo "Mailer Error: " . $mail->ErrorInfo;
                          }
                          else { echo "Message was sent";} */
                    }
                }//end role type

                $task->task_status_id = $id_status;
                $task->Close_Date = date('y-m-d');
                $time = getdate(time());
                $task->Close_Time = $time['hours'] . ':' . $time['minutes'] . ':' . $time['seconds'];
                $task->Close_Name = $session;
                $task->update();
            }//task status_id
        }//change_status_task
        if ($_POST['do'] == 'change_status') {
            $id_status = $_POST['id_status'];
            $id_project = $_POST['id_project'];
            $stringstatus = sprintf("UPDATE `project_status` SET 
				            		 `project_status`=$id_status
									  WHERE  project_id=$id_project ");
            $querystatus = mysql_query($stringstatus) or die(mysql_error() . ' ' . $stringstatus);
        }//change_status

        if ($_POST['do'] == 'get_tasks_open') {//כל הפניות הפתוחות לפי משתמש
            $id_user = $_POST['id'];
            $user = new user();
            $user->getby_id($id_user);
            $arr_project = $user->get_proj_permissions();

            foreach ($arr_project as $idp) {
                $project = new project();
                $project->getby_id($idp);

                if ($project->project_status != 1) {
                    $arr_task = $project->get_tasks();
                    if ($arr_task != 0) {
                        $bool = false;


                        foreach ($arr_task as $idt) {

                            $task = new task();
                            $task->getby_id($idt['task_id']);
                            if (($task->task_status_id == 2 || $task->task_status_id == 3) && ($task->task_project_status != 1)) {
                                if ($bool == false) {
                                    $project_div = "project_" . $idp;
                                    ?>
                                    <div id="<?php echo $project_div ?> ">
                                        <p class="project_title">
                                        <?php echo $project->project_name . '-' . $project->get_customer_name($project->project_customer); ?>
                                        </p>

                                        <?php
                                        $bool = true;
                                    }//if bool
                                    $task->get_html_task();
                                }//task_status_id
                        }//foreach arr_task
                        ?> 
                    </div><?php
                } //end_arr task
            }//end if $project->project_status!=1
        }//foreach_arr project
    }//get_tasks_open
        if ($_POST['do'] == 'insert_message') {
            $id_task = $_POST['id_task'];
            $content = htmlspecialchars($_POST['content'], ENT_COMPAT);

            $user_id = $_POST['user_id'];
            $com = new comments();
            $com->user_id = $user_id;
            $com->task_id = $id_task;
            $com->comment_content = $content;

            $com->insert();

            $task = new task();
            $task->getby_id($id_task);
            $path = './upload/' . $task->project_id . '/';
            $txt = './upload/' . $_SESSION['user'] . '-' . $_SERVER['REMOTE_ADDR'] . 'com.txt';

            if (is_file($txt)) {
                $filename = $txt;
                $handle = fopen($filename, "rb");
                $contents = fread($handle, filesize($filename));
                fclose($handle);
                $arr = explode('/', $contents);


                $new_path = './' . $arr[1] . '/' . $arr[2] . '/' . 'C-' . $id_task . '-' . $com->getcomment_id() . $arr[3];


                rename($contents, $new_path);
                $com->attachments = $new_path;
                $com->update();
                unlink($txt);
            } else {
                // echo "no file";	 
            }
            $temp_user = new user;

            $temp_user->getby_name($_SESSION['user']); //המשתמש הנוכחי
            /*             * ***************************שליחת מייל למנהל המערכת ******************************************** */
            if ($temp_user->role_type != 1) {
                $mail = new PHPMailer();
                $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                $mail->SMTPAuth = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port = 465;                   // set the SMTP port

                $mail->Username = "do_not_reply@restartit.co.il";  // GMAIL username
                $mail->Password = "restart4mail";            // GMAIL password

                $mail->CharSet = "utf-8";
                // $mail->Host = "smtp.email.com"; // SMTP כתובת שרת ה
                $mail->From = $mail_crm; // כתובת השולח
                $mail->AddAddress($mail_meneger); // הכתובת אליה נשלח המייל
                $task = new task();
                $task->getby_id($id_task);
                $number_task = $task->get_task_id();

                $name_project = project::get_name($task->project_id);
                $name_user = user::get_name($user_id);
                $r = "<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
	         <tr><td> פרוייקט</td><td>$name_project</td></tr>
			 <tr><td> מספר פנייה</td><td>$number_task</td></tr>
			 <tr><td> מאת</td><td>$name_user</td></tr>
			 <tr><td> תגובה</td><td>$content</td></tr>
		 </table>";
                $mail->FromName = "RestartIT CRM";
                $mail->Subject = "התקבלה תגובה חדשה  לפניה $number_task על ידי $name_user לפרוייקט $name_project  "; // נושא הדוא"ל
                $mail->Body = $r;
                //$mail->WordWrap = 50;// אני מאפשר רק 50 תוים בשורה
                $mail->IsHTML(true);
                /* if(!$mail->Send()) // כאן הדוא"ל נשלח
                  {
                  // echo "Message was not sent";
                  //echo "Mailer Error: " . $mail->ErrorInfo;

                  }
                  else
                  {
                  //echo "Message has been sent";

                  } */
            }
            /*             * **************************** */
            $project = new project();
            $project->getby_id($task->project_id);
            $permi = $project->getby_project_id();

            if ($permi != 0) {
                $mail = new PHPMailer();
                foreach ($permi as $singlepermi) {
                    $user_temp = new user;
                    $user_temp->getby_id($singlepermi->user_id);

                    if ($user_temp->send_mail == 1 && $temp_user->role_type != $user_temp->role_type) {
                        $mail->AddAddress($user_temp->user_mail); // הכתובת אליה נשלח המייל
                        $haveAddress = true;
                    }
                }
                $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                $mail->SMTPAuth = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port = 465;                   // set the SMTP port

                $mail->Username = "do_not_reply@restartit.co.il";  // GMAIL username
                $mail->Password = "restart4mail";            // GMAIL password

                $mail->CharSet = "utf-8";
                // $mail->Host = "smtp.email.com"; // SMTP כתובת שרת ה
                $mail->From = $mail_crm; // כתובת השולח

                $task = new task();
                $task->getby_id($id_task);
                $number_task = $task->get_task_id();

                $name_project = project::get_name($task->project_id);
                $name_user = user::get_name($user_id);
                $r = "<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
						 <tr><td> פרוייקט</td><td>$name_project</td></tr>
						 <tr><td> מספר פנייה</td><td>$number_task</td></tr>
						 <tr><td> מאת</td><td>$name_user</td></tr>
						 <tr><td> תגובה</td><td>$content</td></tr>
					 
					 </table>";
                $mail->FromName = "RestartIT CRM";
                $mail->Subject = "התקבלה תגובה חדשה  לפניה $number_task על ידי $name_user לפרוייקט $name_project  "; // נושא הדוא"ל
                $mail->Body = $r;
                //$mail->WordWrap = 50;// אני מאפשר רק 50 תוים בשורה
                $mail->IsHTML(true);
                if ($haveAddress) {
                    /* if(!$mail->Send()) // כאן הדוא"ל נשלח
                      {
                      // echo "Message was not sent";
                      //echo "Mailer Error: " . $mail->ErrorInfo;

                      }
                      else
                      {
                      //echo "Message has been sent";

                      } */
                }
            }
            /*             * ************************************************************************************************************* */
            $com->getby_id($com->getcomment_id());
            return $com->get_html_com();
        }//insert_message
        ?>
        <?php
        if ($_POST['do'] == 'send_new_task') {
            $id_project = $_POST['id_project'];
            $hid_id_user = $_POST['hid_id_user'];
            $content = $_POST['content_textarea'];
            $content = htmlspecialchars($content, ENT_COMPAT);
            $inp_link = $_POST['inp_link'];
            $Browser = $_POST['Browser'];
            $path = './upload/' . $id_project . '/';
            $txt = './upload/' . $_SESSION['user'] . '-' . $_SERVER['REMOTE_ADDR'] . '.txt';
            $new_task = new task();
            $new_task->project_id = $id_project;
            $new_task->user_id = $hid_id_user;
            $new_task->task_content = $content;
            $new_task->task_link = $inp_link;
            $new_task->Browser = $Browser;
            $new_task->task_attachments = '';
            $idProjectStatus = new project();
            $idProjectStatus->getby_id($id_project);
            $new_task->task_project_status = $idProjectStatus->project_status;
            $temp_user = new user();
            $temp_user->getby_id($hid_id_user);
            if ($temp_user->role_type == 1) {
                $new_task->task_status_id = 2;
            }

            $new_task->insert();



            if (is_file($txt)) {
                $filename = $txt;
                $handle = fopen($filename, "rb");
                $contents = fread($handle, filesize($filename));
                fclose($handle);

                $arr = explode('/', $contents);
                $new_path = './' . $arr[1] . '/' . $arr[2] . '/' . 'T-' . $new_task->get_task_id() . $arr[3];
                rename($contents, $new_path);
                $new_task->task_attachments = $new_path;
                $new_task->update();
                unlink($txt);
            }

            /*             * **************************************שליחת מייל למנהל המערכת****************************************************** */
            if ($temp_user->role_type != 1) {
                $mail = new PHPMailer();
                $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                $mail->SMTPAuth = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port = 465;                   // set the SMTP port

                $mail->Username = "do_not_reply@restartit.co.il";  // GMAIL username
                $mail->Password = "restart4mail";            // GMAIL password

                $mail->CharSet = "utf-8";
                $mail->From = $mail_crm; // כתובת השולח
                $mail->AddAddress($mail_meneger); // הכתובת אליה נשלח המייל
                $name_project = project::get_name($id_project);
                $name_user = user::get_name($hid_id_user);
                $number_task = $new_task->get_task_id();
                $mail->FromName = "RestartIT CRM";
                $r = "<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
	         <tr><td> פרוייקט</td><td>$name_project</td></tr>
			 <tr><td> פנייה</td><td>$number_task</td></tr>
			 <tr><td> מאת</td><td>$name_user</td></tr>
			 <tr><td> פנייה</td><td>$content</td></tr>
		 </table>";

                $mail->Subject = "נפתחה פנייה $number_task על ידי $name_user לפרוייקט $name_project "; // נושא הדוא"ל
                $mail->Body = $r;
                $mail->IsHTML(true);
            }// role type

            $project = new project();
            $project->getby_id($new_task->project_id);

            $permi = $project->getby_project_id();

            if ($permi != 0) {
                $mail = new PHPMailer();
                foreach ($permi as $singlepermi) {
                    $user_temp = new user;
                    $user_temp->getby_id($singlepermi->user_id);

                    if ($user_temp->send_mail == 1 && $temp_user->role_type != $user_temp->role_type) {
                        $mail->AddAddress($user_temp->user_mail); // הכתובת אליה נשלח המייל
                        $haveAddress = true;
                    }
                }
                $mail->IsSMTP(); // SMTPכאן אני אומר למחלקה להשתמש ב
                $mail->SMTPAuth = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port = 465;                   // set the SMTP port

                $mail->Username = "do_not_reply@restartit.co.il";  // GMAIL username
                $mail->Password = "restart4mail";            // GMAIL password

                $mail->CharSet = "utf-8";
                // $mail->Host = "smtp.email.com"; // SMTP כתובת שרת ה
                $mail->From = $mail_crm; // כתובת השולח

                $task = new task();
                $task->getby_id($new_task->get_task_id());
                $number_task = $task->get_task_id();

                $name_project = project::get_name($task->project_id);
                $name_user = user::get_name($hid_id_user);
                $r = "<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
						 <tr><td> פרוייקט</td><td>$name_project</td></tr>
						 <tr><td> פנייה</td><td>$number_task</td></tr>
						 <tr><td> מאת</td><td>$name_user</td></tr>
						 <tr><td> פנייה</td><td>$content</td></tr>
					 </table>";

                $mail->Subject = "נפתחה פנייה $number_task על ידי $name_user לפרוייקט $name_project "; // נושא הדוא"ל
                $mail->Body = $r;
                $mail->FromName = "RestartIT CRM";
                $mail->IsHTML(true);
            }//if_permi
            $new_task->getby_id($new_task->get_task_id());
            return $new_task->get_html_task();
        }//send_new_task
        ?>